﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 10/04/2009
// Time: 20:00
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;

namespace csharp.rad.general.entries
{
	/// <summary>
	/// Description of frmRegisterProductSubGroup.
	/// </summary>
	public partial class frmRegisterProductSubGroup : common.RegisterTemplate
	{
		ProductSubGroup productSubGroup = new ProductSubGroup();
		ProductSubGroups listSubGroups = new ProductSubGroups();
		ProductGroups listGroups = new ProductGroups();
		
		public frmRegisterProductSubGroup()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
		}
		
		#region overrided methods
			protected override void newEvent()
			{
				productSubGroup.clear();
				txtName.Focus();
			}
			
			/// <summary>
			/// Set data from edits to class
			/// </summary>
			protected override void setClass()
			{
				productSubGroup.name = txtName.Text;
				productSubGroup.GroupId = (cmbProductGroup.SelectedItem as ProductGroup).id;
			}
			
			/// <summary>
			/// Set data from class to edits
			/// </summary>
			protected override void setEdits()
			{
				productSubGroup.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
				
				if (productSubGroup.get())
				{
					txtName.Text = productSubGroup.name;
					setSelectIndexCombo(productSubGroup.GroupId);
				}
			}
			
			/// <summary>
			/// Fill the DataGridView
			/// </summary>
			protected override void fillDataGridView()
			{
				int tam;
				listSubGroups.fillList();
				
				tam = listSubGroups.count();
				dgvMain.Rows.Clear();
				for (int i=0; i < tam; i++)
				{
					listGroups.search(listSubGroups.get(i).GroupId);
					dgvMain.Rows.Add(new String[] {listSubGroups.get(i).id, listSubGroups.get(i).name, listGroups.get().name});
				}				
			}
			
			/// <summary>
			/// Find the object of search
			/// </summary>
			protected override void find()
			{
				common.Util.searchInGrid(dgvMain, "name", txtSearch.Text);
			}		
			
		   /// <summary>
		   /// Save the register in the database.
		   /// </summary>
		   /// <returns>bool - true if the register was saved sucessful, false case not.</returns>	
		   protected override bool save()
		   {
		   	return productSubGroup.save();
		   }
		   
		   /// <summary>
		   /// Update the register in the database
		   /// </summary>
		   /// <returns>bool - true if the register was updated sucessful, false case not.</returns>
		   protected override bool update()
		   {   
		   	return productSubGroup.update();
		   }
		   
		   /// <summary>
		   /// Delete the register in the database.
		   /// </summary>
		   /// <returns>bool - true if the register was deleted sucessful, false case not.</returns>
		   protected override bool delete()
		   {
		   	productSubGroup.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();	
		   	return productSubGroup.delete();
		   }
		   
		   /// <summary>
		   /// Method call in initialization
		   /// </summary>
		   protected override void init()
		   {
		   	DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
		   	DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
		   	DataGridViewTextBoxColumn group = new DataGridViewTextBoxColumn();
		   	
		   	id.Name = "id";
		   	id.Visible = false;
		   	
		   	name.Name = "name";
		   	name.HeaderText = "Nome";
		   	name.Width = 250;
		   	
		   	group.Name = "group";
		   	group.HeaderText = "Grupo";
		   	group.Width = 150;
		   	
		   	dgvMain.Columns.AddRange(new DataGridViewColumn[] {id, name, group});
				
		   	listGroups.fillList();
		   	fillComboGroups();
		   }
		#endregion		
		
		public void setSelectIndexCombo(String value)
		{
			for (int i=0; i < cmbProductGroup.Items.Count; i++)
				if (value.Equals((cmbProductGroup.Items[i] as ProductGroup).id))
				{
					cmbProductGroup.SelectedIndex= i;
					break;
				}					
		}			
	
		public void fillComboGroups()
		{
			for (int i=0; i < listGroups.count(); i++)
				cmbProductGroup.Items.Add(listGroups.get(i));
			cmbProductGroup.DisplayMember = "name";
			cmbProductGroup.ValueMember = "id";
		}
	}
}
